package com.aaa.sbm.service.impl;

import com.aaa.sbm.common.ResultUtil;
import com.aaa.sbm.dao.StockInDetailDao;
import com.aaa.sbm.dao.StockOutDetailDao;
import com.aaa.sbm.entity.Statistics;
import com.aaa.sbm.entity.StockInDetail;
import com.aaa.sbm.entity.StockOutDetail;
import com.aaa.sbm.service.StatisticsService;
import com.aaa.sbm.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private StockInDetailDao stockInDetailDao;

    @Resource
    private StockOutDetailDao stockOutDetailDao;

    @Override
    public Result getDailyStatistics(String date) {
        Statistics statistics = new Statistics();
        List<StockInDetail> inDetails = stockInDetailDao.findByCreateTime(date);
//        计算入库总数和总金额
        Integer inTotalCount = inDetails.stream().mapToInt(StockInDetail::getQuantity).sum();
        double inTotalAmount = inDetails.stream().mapToDouble(StockInDetail::getAmount).sum();

        List<StockOutDetail> outDetails = stockOutDetailDao.findByCreateTime(date);
//        计算出库总数和总金额
        Integer outTotalCount = outDetails.stream().mapToInt(StockOutDetail::getQuantity).sum();
        double outTotalAmount = outDetails.stream().mapToDouble(StockOutDetail::getAmount).sum();

        statistics.setInTotalCount(inTotalCount);
        statistics.setInTotalAmount(inTotalAmount);
        statistics.setOutTotalCount(outTotalCount);
        statistics.setOutTotalAmount(outTotalAmount);
        statistics.setInDetailList(inDetails);
        statistics.setOutDetailList(outDetails);
        return ResultUtil.success(statistics);
    }
}
